package initialize

import (
	"encoding/json"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"

	"github.com/nacos-group/nacos-sdk-go/vo"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"mxshop_api/user-web/global"
)
func GetEnvInfo(env string)bool{
	viper.AutomaticEnv()
	return viper.GetBool(env)
}

func InitConfig(){
	debug :=GetEnvInfo("MXSHOP_DEBUG")
	configFilePrefix :="config"
	configFileName :=fmt.Sprintf("user-web/%s-pro.yaml",configFilePrefix)
	if debug{
		configFileName =fmt.Sprintf("user-web/%s-debug.yaml",configFilePrefix)
	}
	v :=viper.New()
	v.SetConfigFile(configFileName)
	if err :=v.ReadInConfig();err !=nil{
		panic("读取失败")
	}
	if err :=v.Unmarshal(global.NacosConfig);err !=nil{
		panic(err)
	}
	zap.S().Infof("配置信息: %v",global.NacosConfig)


	sc :=[]constant.ServerConfig{
		{
			IpAddr: global.NacosConfig.Host,
			Port: global.NacosConfig.Port,
		},
	}
	cc :=constant.ClientConfig{
		NamespaceId:         global.NacosConfig.NameSpace, // 如果需要支持多namespace，我们可以场景多个client,它们有不同的NamespaceId
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log",
		CacheDir:            "tmp/nacos/cache",
		RotateTime:          "1h",
		MaxAge:              3,
		LogLevel:            "debug",
	}
	configClient,err :=clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs":sc,
		"clientConfig":cc,
	})
	if err != nil {
		panic(err)
	}
	content,err :=configClient.GetConfig(vo.ConfigParam{
		DataId: global.NacosConfig.DataId,
		Group: global.NacosConfig.Group,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(content)
	err = json.Unmarshal([]byte(content),&global.ServerConfig)
	if err !=nil{
		zap.S().Fatalf("读取nacos配置文件失败",err.Error())
	}
	fmt.Println(&global.ServerConfig)
	//fmt.Printf("%v",v.Get("name"))

	//v.WatchConfig()
	//v.OnConfigChange(func(e fsnotify.Event) {
	//	fmt.Println("config file channed :",e.Name)
	//	_=v.ReadInConfig()
	//	_ = v.Unmarshal(&serverConfig)
	//	fmt.Println(serverConfig)
	//})
}


